﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Core.DAL;

namespace xManagerWebApp
{
    public partial class ExpenseReportLineControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //btnDelete.Attributes.Add("onclick", "javascript:if(confirm('Are you sure?')== false) return false;");
        }

        protected void OnLinqDataSourceSelecting(object sender, LinqDataSourceSelectEventArgs e)
        {
            if (this.ExpenseReportId.HasValue)
            {
                using (DataAccessLayer dal = new DataAccessLayer())
                {
                    var query = Core.ExpenseLine.LoadSearchResult(dal, this.ExpenseReportId.Value);
                    e.Result = query;
                }
            }
            else
                e.Result = new List<Core.ExpenseLine>();
        }

        private void SetEditMode(bool mode)
        {
            this.editPanel.Visible = mode;
            this.listPanel.Visible = !mode;
        }

        public override void DataBind()
        {
            ControlExpenseLine1.DataBind();
            this.grid.DataBind();
        }

        public Guid? ExpenseReportId
        {
            get
            {
                if (ViewState["ExpenseReportId"] != null)
                    return (Guid)ViewState["ExpenseReportId"];
                else
                    return null;
            }
            set { ViewState["ExpenseReportId"] = value.Value; }
        }

        #region EventHandlers
        protected void OnSearch(object sender, EventArgs e)
        {
            this.grid.DataBind();
        }

        protected void OnSelected(object sender, EventArgs e)
        {
            Guid id = (Guid)grid.DataKeys[grid.SelectedIndex].Value;
            this.ViewState["PK"] = id;

            //Set the edit control Id here...
            this.ControlExpenseLine1.InstanceId = id;
            //Para informar o controlo que não é uma linha nova, ou seja é modo Edit
            
            this.ControlExpenseLine1.FirstAccess = true;

            this.SetEditMode(true);

        }

        protected void OnSave(object sender, EventArgs e)
        {
            //Call Edit control Save()
            this.ControlExpenseLine1.Save();

            this.SetEditMode(false);
            this.DataBind();
        }

        protected void OnReturn(object sender, EventArgs e)
        {
            this.SetEditMode(false);
        }

        protected void OnNew(object sender, EventArgs e)
        {
            this.SetEditMode(true);
            this.ControlExpenseLine1.InstanceId = null;
            //Para informar o controlo que é uma Nova linha
           
            this.ControlExpenseLine1.FirstAccess = true;

            if (!this.ExpenseReportId.HasValue)
            {
                return;
                //throw new Exception("Cannot create an expense line in a report that hasn't been saved.");
            }

            // Set the expense report id. Without this, no line can be saved.
            this.ControlExpenseLine1.ExpenseReportId = this.ExpenseReportId.Value;
        }

        protected void OnDelete(object sender, EventArgs e)
        {
            ImageButton imageButton = (ImageButton)sender;
            Guid itemId = new Guid(imageButton.CommandArgument);
            using (DataAccessLayer dal = new DataAccessLayer())
            {
                Core.ExpenseLine.Delete(dal, itemId);
                grid.DataBind();
            }
        }


        #endregion
    }
}